<template>
    <view>
        <view v-if="!isLoad" class="margin-top load loading text-l text-admin"></view>

        <view class="main-admin" v-if="isLoad && isAdmin">
            <view class="admin-info bg-admin">
                <view class="pic"><text class="icon-peoplefill"></text></view>
                <view class="right">
                    <view class="name">
                        <text class="text-xl">{{ admin.name }}</text>

                        <text v-if="admin.type == 1" class="tag small bg-orange light radius margin-left-s">超级管理员</text>
                        <text v-if="admin.type == 0" class="tag small bg-green radius margin-left-s">一般管理员</text>
                    </view>
                    <view class="desc text-s">共登录{{ admin.cnt }}次，上次{{ admin.last }}</view>
                </view>
                <view class="exit-admin" @tap="bindExitTap"><text class="icon-exit"></text></view>
            </view>

            <view class="comm-list grid col-4">
                <view class="item" @tap="url" data-url="../../meet/list/admin_meet_list">
                    {{ data.meetCnt || '...' }}
                    <text class="text-black">活动/预约数</text>
                </view>
                <view class="item" @tap="url" data-url="../../info/admin_info_list">
                    {{ data.joinCnt || '...' }}
                    <text class="text-black">预约数</text>
                </view>
                <view class="item" @tap="url" data-url="../../user/list/admin_user_list">
                    {{ data.userCnt || '...' }}
                    <text class="text-black">用户</text>
                </view>
                <view class="item" @tap="url" data-url="../../news/list/admin_news_list">
                    {{ data.newsCnt || '...' }}
                    <text class="text-black">文章数</text>
                </view>
            </view>

            <view class="bar bg-white solid-bottom margin-top-s">
                <view class="action">
                    <text class="icon-title"></text>
                    功能管理
                </view>
            </view>

            <view class="comm-list grid col-3">
                <view class="item" @tap="url" data-url="../../meet/list/admin_meet_list">
                    <text class="icon-activityfill text-blue"></text>
                    <text class="text-black">活动/预约管理</text>
                </view>
                <view class="item" @tap="url" data-url="../../user/list/admin_user_list">
                    <text class="icon-peoplefill text-green"></text>
                    <text class="text-black">用户管理</text>
                </view>
                <view class="item" @tap="url" data-url="../../news/list/admin_news_list">
                    <text class="icon-newsfill text-darkgreen"></text>
                    <text class="text-black">内容管理</text>
                </view>
            </view>

            <!-- BOTTOM begin -->
            <view class="comm-list menu margin-top">
                <view class="item arrow" @tap="url" data-url="../../setup/about/admin_setup_about">
                    <view class="content">
                        <text class="icon-edit text-darkgreen"></text>
                        <text class="text-black">编辑 - 关于我们</text>
                    </view>
                </view>

                <view class="item arrow" @tap="url" data-url="../../setup/contact/admin_setup_contact">
                    <view class="content">
                        <text class="icon-post text-pink"></text>
                        <text class="text-black">编辑 - 联系我们</text>
                    </view>
                </view>

                <view class="item arrow" @tap="url" data-url="../../setup/qr/admin_setup_qr">
                    <view class="content">
                        <text class="icon-qr_code text-mauve"></text>
                        <text class="text-black">小程序二维码</text>
                    </view>
                </view>
            </view>
            <view class="comm-list menu margin-bottom-xl">
                <view class="item arrow" @tap="url" data-url="../../mgr/log/admin_log_list">
                    <view class="content">
                        <text class="icon-footprint text-brown"></text>
                        <text class="text-black">管理员操作日志</text>
                    </view>
                </view>

                <view class="item arrow" @tap="bindSettingTap">
                    <view class="content">
                        <text class="icon-settings text-grey"></text>
                        <text class="text-black">设置</text>
                    </view>
                </view>
            </view>

            <!-- BOTTOM END -->
            <button @tap="bindExitTap" class="btn-admin">退出登录</button>

            <view class="foot-bottom"><cmpt-foot /></view>
        </view>
    </view>
</template>

<script>
import cmptFoot from '../../../../cmpts/biz/foot/foot_cmpt';
const AdminBiz = require('../../../../biz/admin_biz.js');
const pageHelper = require('../../../../helper/page_helper.js');
const cloudHelper = require('../../../../helper/cloud_helper.js');
export default {
    components: {
        cmptFoot
    },
    data() {
        return {
            isLoad: false,

            admin: {
                name: '',
                type: 0,
                cnt: '',
                last: ''
            },

            data: '',
            isAdmin: ''
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        if (!AdminBiz.isAdmin(this)) {
            return;
        }
        this.loadDetailFun();
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: async function () {
        await this.loadDetailFun();
        uni.stopPullDownRefresh();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    methods: {
        loadDetailFun: async function () {
            let admin = AdminBiz.getAdminToken();
            this.setData({
                isLoad: true,
                admin
            });
            try {
                let opts = {
                    title: 'bar'
                };
                let res = await cloudHelper.callCloudData('admin/home', {}, opts);
                this.setData({
                    data: res
                });
            } catch (err) {
                console.log('CatchClause', err);
                console.log('CatchClause', err);
                console.log(err);
            }
        },

        url: function (e) {
            pageHelper.url(e, this);
        },

        bindExitTap: function (e) {
            let callback = function () {
                AdminBiz.clearAdminToken();
                uni.reLaunch({
                    url: pageHelper.fmtURLByPID('/pages/my/index/my_index')
                });
            };
            pageHelper.showConfirm('您确认退出?', callback);
        },

        bindSettingTap: function (e) {
            let itemList = ['清除数据缓存'];
            uni.showActionSheet({
                itemList,
                success: async (res) => {
                    switch (res.tapIndex) {
                        case 0: {
                            //清除缓存
                            await this.clearCacheFun();
                            break;
                        }
                    }
                },
                fail: function (res) {}
            });
        },

        clearCacheFun: async function () {
            try {
                let opts = {
                    title: '数据缓存清除中'
                };
                await cloudHelper.callCloudSumbit('admin/clear_cache', {}, opts).then((res) => {
                    pageHelper.showSuccToast('清除成功');
                });
            } catch (err) {
                console.log('CatchClause', err);
                console.log('CatchClause', err);
                console.error(err);
            }
        }
    }
};
</script>
<style>
@import './admin_home.css';
</style>
